package com.cn.wanxi.util;

import java.io.*;
import java.util.List;

public class IO {

    static File file = null;

    public static void createFile(String fileName) {
        String path = "";
        File filePath = new File(path);
        boolean exists = filePath.exists();
//        System.out.println(exists);
        if (!exists) {
            path = FileName.FILE_PATH;
            filePath = new File(path);
        }
//        少些if else  可以if
//      创建包，文件夹
        String directory = "note";
        File fileDirectory = new File(filePath, directory);
        if (!fileDirectory.exists()) {
            boolean mkdir = fileDirectory.mkdirs();
            System.out.println(mkdir);
        }
//        String fileName = "company.txt";
        File tempFile = new File(fileDirectory, fileName);
        try {
            tempFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        file = tempFile;
//        return file;
    }

    /**
     * 写注释
     *
     * @param data
     */
    public static void writeData(List<String> data) {
        try {
            OutputStream outputStream = new FileOutputStream(file);

            for (int i = 0; i < data.size(); i++) {
                outputStream.write(data.get(i).getBytes());
                outputStream.write(FileName.FILE_SPLIT.getBytes());
            }

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取数据
     *
     * @return
     */
    public static String readData() {
        StringBuilder reuslt = new StringBuilder();
        try {
            Reader reader = new FileReader(file);
            int rr = 0;
            while ((rr = reader.read()) != -1) {
                reuslt.append((char) rr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return String.valueOf(reuslt);
    }

}
